*** Replication materials for:
*** Cox, Gary W., J. H. Fiva and D. M. Smith
*** "Parties, Legislators, and the Origins of Proportional Representation"
*** Comparative Political Studies

/* the following packages needs to be installed */
*ssc install estout
*ssc install binscatter
*ssc install firthlogit

local path = "C:\Users\a0910846\Dropbox\CoxFivaSmith\CoxFivaSmith_CPS"
cd `path'
cap mkdir "`path'\tables"
cap mkdir "`path'\figures"
cap mkdir "`path'\figures\gph"

**** The following data files are needed
* Figure1Data.dta 							 /* Voteshares and seatshares across blocs */
* Voting1919.dta                            /* Roll call votes for six different reform proposals */
* EldreVoteringerPerson.dta                /* Voteringsarkivet 1814-1940 - part I http://www.nsd.uib.no/polsys/index.cfm?urlname=polsys&lan=&institusjonsnr=1&arkivnr=26&MenuItem=N1_1&ChildItem=&State=collapse */
* EldreVoteringerSak.dta                   /* Voteringsarkivet 1814-1940 - part II http://www.nsd.uib.no/polsys/index.cfm?urlname=polsys&lan=&institusjonsnr=1&arkivnr=26&MenuItem=N1_1&ChildItem=&State=collapse */
* DataSet_Elected1906_1936                 /* Candidates elected in the 1906-1936 from the Fiva and Smith (2017) dataset https://search.nsd.no/study/NSD2405 */

**********************************************************************************************
******************* Data preperation  ********************************************************
**********************************************************************************************
run do\Datapreparation.do 

**********************************************************************************************
******************* Figure 1: Voteshares and Seatshares Across Blocs  ************************
**********************************************************************************************
use dta/Figure1Data, clear
foreach bloc in Labor Liberals Conservatives Other {
twoway (scatter voteshare year if bloc=="`bloc'", connect(l) lpattern(dash) msymbol(circle) mcolor(white) mlcolor(black)) (scatter seatshare year if bloc=="`bloc'", connect(l) msymbol(circle) mcolor(black)) ///
, xline(1919.5, lpattern(dash)) xtitle("") ytitle("Proportion") title(`bloc') plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono)  yscale(range(0 0.65)) ylabel(0[0.1]0.6) legend(order(1 "Vote shares" 2 "Seat shares"))
graph save figures/gph/vsh_sh_`bloc'.gph, replace
}
cd figures/gph
grc1leg vsh_sh_Labor.gph vsh_sh_Liberals.gph vsh_sh_Conservatives.gph vsh_sh_Other.gph, plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono)  
graph export ../Figure1.pdf, replace
cd ../../

**********************************************************************************************
******************* Figure 2: First-round support for the Labor party in 1918  ***************
**********************************************************************************************
use dta/Voting1919.dta, clear

hist SocialistVS_unstand, freq width(0.05) start(0) bfcolor(gs8) blcolor(gs6) ///
plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono) xlabel(0(0.1)0.8) xtitle(Socialist vote share) title(All districts)
graph save figures/gph/SocialistVS_unstand_a.gph, replace

hist SocialistVS_unstand if party=="h_fv" | party=="v_a", freq width(0.05) start(0) bfcolor(gs8) blcolor(gs6) ///
plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono) xlabel(0(0.1)0.8) ///
xtitle(Socialist vote share) title(Districts won by Liberal or Conservatives)
graph save figures/gph/SocialistVS_unstand_b.gph, replace

cd figures/gph/
graph combine SocialistVS_unstand_a.gph SocialistVS_unstand_b.gph, ycommon rows(2) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono) 
graph export ../Figure2.pdf, replace
cd ../../


**********************************************************************************************
******************* Figure 3: Support for reform proposals against socialist threat***********
**********************************************************************************************
use dta/Voting1919.dta, clear

binscatter ReformYes_Postpone SocialistVS if party=="h_fv" | party=="v_a", nq(10) title(Postpone) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) ytitle("Fraction voting in favor") xtitle("Socialist vote share") msymbol(Oh) mcolor(black) lcolor(black)
graph save figures\gph\_ReformYes_Postpone.gph, replace

binscatter ReformYes_A_viii SocialistVS if party=="h_fv" | party=="v_a", nq(10) title(Proposal A) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) ytitle("Fraction voting in favor") xtitle("Socialist vote share") msymbol(Oh) mcolor(black) lcolor(black)
graph save figures\gph\_ReformYes_A_viii.gph, replace

binscatter ReformYes_A_vi SocialistVS if party=="h_fv" | party=="v_a", nq(10) title(Proposal B) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) ytitle("Fraction voting in favor") xtitle("Socialist vote share") msymbol(Oh) mcolor(black) lcolor(black)
graph save figures\gph\_ReformYes_A_vi.gph, replace

binscatter ReformYes_A_ii SocialistVS if party=="h_fv" | party=="v_a", nq(10) title(Proposal C) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) ytitle("Fraction voting in favor") xtitle("Socialist vote share") msymbol(Oh) mcolor(black) lcolor(black)
graph save figures\gph\_ReformYes_A_ii.gph, replace

binscatter ReformYes_B_i SocialistVS if party=="h_fv" | party=="v_a", nq(10) title(Proposal D) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) ytitle("Fraction voting in favor") xtitle("Socialist vote share") msymbol(Oh) mcolor(black) lcolor(black)
graph save figures\gph\_ReformYes_B_i.gph, replace

binscatter ReformYes_A_iv SocialistVS if party=="h_fv" | party=="v_a", nq(10) title(Proposal E) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) ytitle("Fraction voting in favor") xtitle("Socialist vote share") msymbol(Oh) mcolor(black) lcolor(black)
graph save figures\gph\_ReformYes_A_iv.gph, replace

cd figures\gph
graph combine _ReformYes_Postpone.gph _ReformYes_A_viii.gph _ReformYes_A_vi.gph _ReformYes_A_ii.gph _ReformYes_B_i.gph _ReformYes_A_iv.gph , ///
ycommon xcommon title("")  plotregion(lcolor(white) ilcolor(white)) graphregion(color(white))  scheme(s2mono)
graph export ..\Figure3.pdf, replace
cd ..\..\

**********************************************************************************************
******************* Table 1: Descriptives across blocs  **************************************
**********************************************************************************************
use dta/Voting1919, clear
sum Reform*
sum Reform* if party=="s"
sum Reform* if party=="v_a"
sum Reform* if party=="h_fv"

**********************************************************************************************
******************* Table 2: Descriptives political careers  *********************************
**********************************************************************************************
use dta/Voting1919.dta, clear
tab1 *_ever CabinetPost if PartyElitePre==0 & (party=="h_fv" | party=="v_a")
tab1 *_ever CabinetPost if PartyElitePre==1 & (party=="h_fv" | party=="v_a")
tab party PartyElitePre if party=="h_fv" | party=="v_a"
sum *_ever* CabinetPost SocialistVS_unstand if PartyElitePre==0 & (party=="h_fv" | party=="v_a")
sum *_ever* CabinetPost SocialistVS_unstand if PartyElitePre==1 & (party=="h_fv" | party=="v_a")

**********************************************************************************************
******************* Table 3: Results from LPM of voting for reform proposals *****************
**********************************************************************************************
use dta/Voting1919.dta, clear
eststo clear
foreach var in Postpone A_vi A_ii B_i A_iv {
reg ReformYes_`var' PartyElitePre Conservative SocialistVS if party=="h_fv" | party=="v_a", vce(hc3)
eststo
}
esttab using tables\Table3.tex, replace se b(%9.3f) se(%9.3f) r2 star(* 0.10 ** 0.05 *** 0.01) coeflabels(SocialistVS "Socialist vote share" Conservative "Conservative MP" PartyElitePre "Party elite" _cons "Constant") ///
nodepvars mtitles("Postpone" "B" "C" "D" "E") prehead("\begin{tabular}{l*{@M}{rr}}" "\hline") posthead(\hline) postfoot("\hline" "\end{tabular}")  


**********************************************************************************************
******************* Table 4: PR and voting cohesion ******************************************
**********************************************************************************************
use dta/Voteringsarkivet, clear
eststo clear
foreach bloc in Labor Liberal Conservative {
reg deviation PRadopt PRimplement odelsting zz* Halvorsen1 Blehr Halvorsen2 tax-budget if partybloc=="`bloc'", cluster(lpnr)
eststo
}
esttab using tables\Table4.tex, replace se b(%9.3f) se(%9.3f) r2 star(* 0.10 ** 0.05 *** 0.01) mtitles("Labor" "Liberal" "Conservative")  ///
coeflabels (odelsting "Odelsting" Halvorsen1Cabinet "Halvorsen I cabinet (Con.)" BlehrCabinet "Blehr cabinet (Lib.)" Halvorsen2Cabinet "Halvorsen II cabinet (Con.)") ///
drop(*zz* _cons tax agriculture railway military school alcohol socialwelfare constitution budget) nodepvars prehead("\begin{tabular}{l*{@M}{rr}}" "\hline") posthead(\hline) postfoot("\hline" "\end{tabular}")  


**********************************************************************************************
******************* Appendix Figure 1: Time trend in deviation, by party bloc ****************
**********************************************************************************************

use dta/Voteringsarkivet_Yearly, clear
foreach var in deviation {
	foreach bloc in Labor Conservative Liberal {
	twoway (scatter `var' votingyear if partybloc=="`bloc'" & votingyear<1922, msymbol(circle) mcolor(white) mlcolor(black)) (scatter `var' votingyear if partybloc=="`bloc'" & votingyear>1921, msymbol(circle) mcolor(white) mlcolor(black)),  title(`bloc') xtitle("") ytitle("Average deviation") xline(1921.5, lpattern(dash) lcolor(black)) xline(1912.5 1915.5 1918.5 1924.5 1927.5 1930.5 1933.5, lpattern(dash) lcolor(gray*0.5)) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono) xscale(range(1910 1936)) legend(off)
	graph save figures/gph/_`bloc'_`var'.gph, replace
	}

cd figures/gph/
graph combine _Labor_`var'.gph _Liberal_`var'.gph _Conservative_`var'.gph , ycommon xcommon title("") plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono) 
cd ../../
}
graph export figures/FigureA1.pdf, replace

**********************************************************************************************
******************* Appendix Figure 2: Deviation before and after PR adoption     ************
**********************************************************************************************
use dta/Voteringsarkivet, clear
gen zz=votnr_new+126
gen qq=zz+38
keep if KnudsenCabinet==1
keep if storting==1

foreach var in deviation {
	foreach bloc in Labor Conservative Liberal {
	binscatter `var' zz if partybloc=="`bloc'", rd(0) xq(qq) title(`bloc',color(black)) ytitle("Average deviation") xtitle(Vote number,color(black))  plotregion(lcolor(white) ilcolor(white)) graphregion(color(white))  msymbol(Oh) mcolor(black) lcolor(black)
	graph save figures/gph/binscatter`bloc'_`var'.gph, replace
	}
cd figures/gph/
graph combine binscatterLabor_`var'.gph binscatterLiberal_`var'.gph binscatterConservative_`var'.gph, ycommon xcommon title("") plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono) 
cd ../../
graph export figures/FigureA2.pdf, replace 
}


**********************************************************************************************
******************* Appendix Figure 3: Deviation before and after PR implementation **********
**********************************************************************************************

use dta/Voteringsarkivet, clear

gen zz=votnr_new+126
gen qq=zz+38
keep if BlehrCabinet==1
keep if storting==1

foreach var in deviation {
	foreach bloc in Labor Conservative Liberal {
	binscatter `var' votnr_new if partybloc=="`bloc'", rd(-0.5) xq(qq) title(`bloc',color(black)) ytitle("Average deviation") xtitle(Vote number,color(black)) plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) msymbol(Oh) mcolor(black) lcolor(black)
	graph save figures/gph/binscatter`bloc'_`var'.gph, replace
	}
cd figures/gph/
graph combine binscatterLabor_`var'.gph binscatterLiberal_`var'.gph binscatterConservative_`var'.gph, ycommon xcommon title("") plotregion(lcolor(white) ilcolor(white)) graphregion(color(white)) scheme(s2mono)
cd ../../
graph export figures/FigureA3.pdf, replace 
}



**********************************************************************************************
******************* Appendix Table 1: Sensitivity analysis; add deputies to elite ************
**********************************************************************************************
use dta/Voting1919.dta, clear

replace PartyElitePre=1 if candidatename_ed=="Nils Magnus Nilsen Tvedten" /* deputy for Olsen Nalum */
replace PartyElitePre=1 if candidatename_ed=="Nils Gregoriussen Skilbred" /* deputy for G. Knudsen */
replace PartyElitePre=1 if candidatename_ed=="Albert Fredrik Eggen" /* deputy for Aavatsmark */
replace PartyElitePre=1 if candidatename_ed=="Peder Aslak Berntsen Owren" /* deputy for Castberg */

eststo clear
foreach var in Postpone A_vi A_ii B_i A_iv {
reg ReformYes_`var' PartyElitePre Conservative SocialistVS if party=="h_fv" | party=="v_a", vce(hc3)
eststo
}
esttab using tables\TableA1.tex, replace se b(%9.3f) se(%9.3f) r2 star(* 0.10 ** 0.05 *** 0.01) coeflabels(SocialistVS "Socialist vote share" Conservative "Conservative MP" PartyElitePre "Party elite" _cons "Constant") ///
nodepvars mtitles("Postpone" "B" "C" "D" "E") prehead("\begin{tabular}{l*{@M}{rr}}" "\hline") posthead(\hline) postfoot("\hline" "\end{tabular}")  


**********************************************************************************************
******************* Appendix Table 2: Sensitivity analysis; firthlogit ***********************
**********************************************************************************************
use dta/Voting1919.dta, clear

replace PartyElitePre=1 if candidatename_ed=="Nils Magnus Nilsen Tvedten" /* deputy for Olsen Nalum */
replace PartyElitePre=1 if candidatename_ed=="Nils Gregoriussen Skilbred" /* deputy for G. Knudsen */
replace PartyElitePre=1 if candidatename_ed=="Albert Fredrik Eggen" /* deputy for Aavatsmark */
replace PartyElitePre=1 if candidatename_ed=="Peder Aslak Berntsen Owren" /* deputy for Castberg */

eststo clear
foreach var in Postpone A_viii A_vi A_ii B_i A_iv {
firthlogit ReformYes_`var' PartyElitePre Conservative SocialistVS if party=="h_fv" | party=="v_a"
mfx
eststo
}
esttab using tables\TableA2.tex, replace margin se b(%9.3f) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) coeflabels(SocialistVS "Socialist vote share" Conservative "Conservative MP" PartyElitePre "Party elite" _cons "Constant") ///
nodepvars mtitles("Postpone" "A" "B" "C" "D" "E") prehead("\begin{tabular}{l*{@M}{rr}}" "\hline") posthead(\hline) postfoot("\hline" "\end{tabular}")  


**********************************************************************************************
************************* RANDOMIZATION INFERENCE (FOOTNOTE 30) ******************************
**********************************************************************************************
use dta/Voting1919.dta, clear
keep if party=="h_fv"

gen Y=ReformYes_A_iv
gen T=PartyElitePre

keep party Y T
save dta/RandomizationTest.dta, replace
rename T Tactual

****   ACTUAL EFFECT *********
reg Y Tactual, vce(hc3)

**** RANDOMIZATION INFERENCE *********
postfile placebo pointest se using dta/placebo.dta, replace

*forvalues i = 1(1)1000000 {
forvalues i = 1(1)1000 {
preserve
gen randomization=`i'
gen random=runiform()
sort random
gen T=0
replace T=1 in 1/12
qui reg Y T, vce(hc3)
post placebo (_b[T]) (_se[T])
estimates save placebo, replace
restore
}

postclose placebo

use dta/placebo.dta, clear
hist pointest, discrete xline(0.3377193)
gen tstat=pointest/se
hist tstat, discrete xline(2.93)

gen larger=0
replace large=1 if pointest>=0.3377
replace large=1 if pointest<=-0.3377
tab larger
